package sg.edu.nus.comp.icreate.ivletalk.announcement;

import java.util.List;
import sg.edu.nus.comp.icreate.ivletalk.BaseActivity;
import sg.edu.nus.comp.icreate.ivletalk.R;
import sg.edu.nus.comp.icreate.ivletalk.User;
import sg.edu.nus.comp.icreate.ivletalk.module.MainActivity;
import sg.edu.nus.comp.icreate.ivletalk.module.Module;
import sg.edu.nus.comp.icreate.ivletalk.utils.net.DataPuller;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListAnnouncementActivity extends BaseActivity {
	public static final String EXTRA_CONTENT = "content";
	public static final String EXTRA_TITLE = "title";
	public static final String EXTRA_CREATOR = "creator";
	private final String URL_HEAD = "https://ivle.nus.edu.sg/API/Lapi.svc/Announcements?APIKey=" + MainActivity.APIKEY + "&AuthToken="
			+ User.getInstance().getToken() + "&CourseID=";
	private final String URL_TAIL = "&Duration=0&TitleOnly=false&output=xml";
	private List<Announcement> announcements;
	ListView listAnnouncement;
	private Module module;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_announcement);
		Intent intent = getIntent();
		module = (Module) MainActivity.modules.get(intent.getIntExtra(MainActivity.EXTRA_NAME_MODULE_POSITION, 0));
		try {
			announcements = (List<Announcement>) getData(parseURL(module));
		} catch (Exception e) {
			Log.e("error", "Error in get announcement data", e);
		}
		if (announcements.isEmpty()) {
			TextView textView = new TextView(this);
			textView.setTextSize(20);
			textView.setText("There is no announcement.");
			setContentView(textView);
		} else {
			listAnnouncement = (ListView) findViewById(R.id.listAnnouncements);
			ArrayAdapter<Announcement> adapter = new ArrayAdapter<Announcement>(this, android.R.layout.simple_list_item_1, announcements);
			listAnnouncement.setAdapter(adapter);
			listAnnouncement.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					((ListAnnouncementActivity) parent.getContext()).goToItem(position);
				}
			});
		}
	}

	private String parseURL(Module module) {
		return URL_HEAD + module.getId() + URL_TAIL;
	}

	private Object getData(String url) throws Exception {
		DataPuller puller = new DataPuller(new AnnouncementXmlParser());
		puller.execute(url);
		return puller.get();
	}

	public void goToItem(int position) {
		Intent intent = new Intent(this, ViewAnnouncementContentActivity.class);
		intent.putExtra(EXTRA_CONTENT, announcements.get(position).getDescription());
		intent.putExtra(EXTRA_TITLE, announcements.get(position).getTitle());
		intent.putExtra(EXTRA_CREATOR, announcements.get(position).getCreator() + ", " + announcements.get(position).getCreatedDate().replace("T", " "));
		startActivity(intent);
	}

	public List<Announcement> getDisplayedItemList() {
		return announcements;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected boolean handleActionRefresh() {
		try {
			announcements.clear();
			announcements.addAll((List<Announcement>) getData(parseURL(module)));
			((BaseAdapter) listAnnouncement.getAdapter()).notifyDataSetChanged();
		} catch (Exception e) {
			Log.e("error", "Error in handleActionRefresh", e);
		}
		return true;
	}

}
